# -*- coding:utf-8 -*-
from flask import Flask, request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao.departDao import DepartDao

from . import app

CORS(app, supports_credentials=True)

'''项目部门表 zzh_depart API'''


@app.route('/add-depart', methods=['POST'])
def add_depart():
    if not request.json:
        abort(400)
    depart_info = {
        "depart_name": request.json['depart_name'],
        "depart_vid": request.json['depart_vid'],
        "depart_code": request.json['depart_code'],
        "depart_status": request.json.get('depart_status', 1),
        "depart_sort": request.json.get('depart_sort', 0)
    }
    result = DepartDao().add_depart(depart_info)
    if result:
        return jsonify({"departInfo": depart_info, "message": {"code": 1, "info": "添加成功！"}})
    else:
        return jsonify({"message": {"code": 0, "info": "添加失败！"}})


@app.route('/update-depart/<int:depart_id>', methods=['POST'])
def update_depart(depart_id):
    if not request.json:
        abort(400)
    depart_info = {
        "depart_name": request.json['depart_name'],
        "depart_vid": request.json['depart_vid'],
        "depart_code": request.json['depart_code'],
        "depart_status": request.json.get('depart_status', 1),
        "depart_sort": request.json.get('depart_sort', 0)
    }
    result = DepartDao().update_depart(depart_id, depart_info)
    return jsonify({"departInfo": depart_info, "message": {"code": 1, "info": "更新成功！"}})


@app.route('/query-depart', methods=['GET'])
def query_depart():
    query_result = DepartDao().query_depart()

    return jsonify({"departInfo": query_result, "message": {"code": 1, "info": "OK"}})
